package dq.jvm;

import java.lang.reflect.Field;
import java.util.Stack;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: Dang Qi
 * @Date: 2021/3/8  12:50
 * @Description:
 */
public class TestString {
    public static void main(String[] args) throws Exception {
        testReflection();

    }
    public static void testReflection() throws Exception {
        //创建字符串"Hello World"， 并赋给引用s
        String s = "Hello World";
        System.out.println("修改前：s = " + s); //Hello World
        //获取String类中的value字段
        Field valueFieldOfString = String.class.getDeclaredField("value");
        //改变value属性的访问权限
        valueFieldOfString.setAccessible(true);
        //获取s对象上的value属性的值
        char[] value = (char[]) valueFieldOfString.get(s);
        //改变value所引用的数组中的第5个字符
        value[5] = '?';
        System.out.println("修改后：s = " + s);  //Hello_World

        System.out.println("s == \"Hello?World\": " + (s == "Hello?World"));
        System.out.println("s == \"Hello World\": " +( s == "Hello World"));
        System.out.println("s.equals(\"Hello?World\"): " + s.equals("Hello?World"));
        System.out.println("s.equals(\"Hello World\"): " + s.equals("Hello World"));

    }
}
